package com.xuanwei.rabbit_consume.listen;

import com.alibaba.fastjson.JSON;
import com.xuanwei.rabbit_consume.handler.ProductHandler;
import com.xuanwei.xwcommon.consts.MqType;
import com.xuanwei.xwcommon.entity.dto.MessageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = {MqType.PRODUCT})
@Slf4j
public class ProductListen {


    private ProductHandler productHandler;

    public ProductListen(ProductHandler productHandler) {
        this.productHandler = productHandler;
    }

    @RabbitHandler
    public void process(String msg) {
        log.info("receive msg:{}",msg);
        MessageDto dto = JSON.parseObject(msg, MessageDto.class);
        switch (dto.getType()){
            case UPDATE:
                log.info("execute product update to es");
                String u_id = dto.getData().toString();
                productHandler.insert(Integer.parseInt(u_id));
                break;
            case INSERT:
                log.info("execute product insert to es");
                String i_id = dto.getData().toString();
                productHandler.insert(Integer.parseInt(i_id));
                break;
            case DELETE:
                log.info("execute product delete to es");
                String d_id = dto.getData().toString();
                productHandler.delete(Integer.parseInt(d_id));
                break;
            default:
                log.warn("no  type");
        }
    }

}
